package com.centerm.bluetooth.ibridge.constants;

import com.centerm.bluetooth.ibridge.parse.PackageParse;
import com.centerm.bluetooth.model.Respond;

import java.io.IOException;
import java.io.InputStream;

import static com.centerm.bluetooth.ibridge.parse.PackageParse.CentermFormat;
import static com.centerm.bluetooth.ibridge.parse.PackageParse.LKLFormat;

/**
 * Created by linwenhui on 2017/2/9.
 */

public class ChannelConfig {
    public final static int LKL = 1;
    public final static int CENTERM = 2;
    private int channel = CENTERM;
    private static ChannelConfig instance;

    private ChannelConfig() {
    }

    public static ChannelConfig getInstance() {
        if (instance == null) {
            synchronized (ChannelConfig.class) {
                if (instance == null) {
                    instance = new ChannelConfig();
                }
            }
        }
        return instance;
    }

    public void setChannel(int channel) {
        this.channel = channel;
    }

    public int getChannel() {
        return channel;
    }

    public static byte[] requestPack(byte packageId, byte[] cmd, byte[] params) throws IOException {
        switch (ChannelConfig.getInstance().getChannel()) {
            case LKL:
                return PackageParse.LKLFormat.requestPack(packageId, cmd, params);
            case CENTERM:
                return PackageParse.CentermFormat.requestPack(packageId, cmd, params);
        }
        return null;
    }

    public static byte[] read(InputStream in) throws IOException {
        switch (ChannelConfig.getInstance().getChannel()) {
            case ChannelConfig.LKL:
                return LKLFormat.read(in);
            case ChannelConfig.CENTERM:
                return CentermFormat.read(in);
        }
        return null;
    }

    public static Respond respondParse(byte packageId, byte[] resultDatas, byte[] cmd) {
        switch (ChannelConfig.getInstance().getChannel()) {
            case ChannelConfig.LKL:
                return LKLFormat.respondPack(packageId, resultDatas, cmd);
            case ChannelConfig.CENTERM:
                return CentermFormat.respondPack(packageId, resultDatas, cmd);
        }
        return null;
    }

}
